<template>
  <div class="app-container">
    <div class="default-box">
      <div class="filter-container">
        <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-plus" round @click="createDialog = true;isadd = true">
          新增
        </el-button>
        <el-button class="filter-item" icon="el-icon-delete" round @click="recycleDialog = true">
          回收站
        </el-button>
      </div>
      <el-table
        :key="tableKey"
        v-loading="listLoading"
        :data="list"
        fit
        highlight-current-row
        style="width: 100%;"
      >
        <el-table-column label="角色昵称">
          <template>
            <span>章鱼小丸子</span>
          </template>
        </el-table-column>
        <el-table-column label="描述" show-overflow-tooltip>
          <template>
            <span>描述描描述描鱼小丸子描述描子</span>
          </template>
        </el-table-column>
        <el-table-column label="创建时间">
          <template>
            <span>
              <!-- {{ row.timestamp | parseTime('{y}-{m}-{d} {h}:{i}:{s}') }} -->
              2022-10-08 12:42:40
            </span>
          </template>
        </el-table-column>
        <el-table-column label="更新时间">
          <template>
            <span>
              <!-- {{ row.timestamp | parseTime('{y}-{m}-{d} {h}:{i}:{s}') }} -->
              2022-10-08 12:42:40
            </span>
          </template>
        </el-table-column>
        <el-table-column label="操作" class-name="small-padding fixed-width">
          <template>
            <i class="el-icon-edit" :style="{color:$store.state.settings.theme}" @click="createDialog = true;isadd = false" />
            <i class="el-icon-user" :style="{color:$store.state.settings.theme}" @click="rolesDialog = true" />
            <i class="el-icon-delete danger-color" @click="handleDelete" />
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    </div>
    <!-- 展示新增、编辑对话框 -->
    <el-dialog
      :title="isadd?'新增角色':'编辑角色'"
      :visible.sync="createDialog"
      width="50%"
      custom-class="operate-dialog"
      top="35vh"
      center
    >
      <el-form :model="oprateform" label-width="80px">
        <el-form-item label="名称">
          <el-input v-model="oprateform.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="描述">
          <el-input v-model="oprateform.describe" type="textarea" :autosize="{ minRows: 1, maxRows: 4}" placeholder="请输入描述" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleResetOperate">重置</el-button>
        <el-button type="primary" @click="handleOperate">提交</el-button>
      </span>
    </el-dialog>
    <!-- 分配直接权限对话框 -->
    <el-dialog
      title="分配直接权限"
      :visible.sync="rolesDialog"
      width="50%"
      custom-class="operate-dialog"
      top="35vh"
      center
    >
      <el-form :model="oprateform" label-width="80px">
        <el-form-item label="角色名称">
          <el-input v-model="oprateform.name" placeholder="角色名称" />
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input v-model="oprateform.describe" type="textarea" :autosize="{ minRows: 1, maxRows: 4}" placeholder="请输入角色描述" />
        </el-form-item>
        <el-form-item label="权限">
          <el-checkbox v-model="checkAll" :indeterminate="isIndeterminate" @change="handleCheckAllChange">后台权限</el-checkbox>
          <div style="margin: 15px 0;" />
          <el-checkbox-group v-model="checkedRoles" @change="handleCheckedRolesChange">
            <el-checkbox v-for="rolename in Roles" :key="rolename" :label="rolename">{{ rolename }}</el-checkbox>
          </el-checkbox-group>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="rolesDialog = false">重置</el-button>
        <el-button type="primary" @click="handleRoles">提交</el-button>
      </span>
    </el-dialog>
    <!--  回收站对话框 -->
    <el-dialog
      title="回收站"
      :visible.sync="recycleDialog"
      width="80%"
      custom-class="operate-dialog main-bagcolor"
      top="10vh"
      center
    >
      <RecycleRole />
    </el-dialog>
  </div>
</template>

<script>
const rolenameOptions = ['列表', '添加', '编辑', '激活/关闭']
import { fetchList } from '@/api/article'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import RecycleRole from './components/recycle-role'
export default {
  name: 'Adminrole',
  components: { Pagination, RecycleRole },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 10,
        title: ''
      },
      oprateform: { // 新增、编辑条件
        name: '',
        describe: ''
      },
      isadd: true, // 是新增 否编辑
      createDialog: false, // 是否展示新增、编辑对话框
      rolesCheckList: [], // 角色选择数组
      rolesDialog: false, // 角色直接权限对话框
      checkAll: false, // 角色直接权限是否全选
      checkedRoles: ['上海', '北京'],
      Roles: rolenameOptions,
      isIndeterminate: true,
      recycleDialog: false // 回收站对话框
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      fetchList(this.listQuery).then(response => {
        this.list = response.data.items
        this.total = response.data.total

        // Just to simulate the time of the request
        setTimeout(() => {
          this.listLoading = false
        }, 1.5 * 1000)
      })
    },
    // 新增、编辑
    handleOperate() {
      this.createDialog = false
    },
    // 重置
    handlReset() {
      this.listQuery.account = ''
    },
    // 回收站
    handleRecycle() {

    },
    // 角色直接权限
    handleRoles() {

    },
    // 重置新增、编辑角色输入框
    handleResetOperate() {
      this.oprateform.name = ''
      this.oprateform.describe = ''
    },
    handleCheckAllChange(val) {
      this.checkedRoles = val ? rolenameOptions : []
      this.isIndeterminate = false
    },
    handleCheckedRolesChange(value) {
      const checkedCount = value.length
      this.checkAll = checkedCount === this.Roles.length
      this.isIndeterminate = checkedCount > 0 && checkedCount < this.Roles.length
    },
    // 删除
    handleDelete(row, index) {
      this.$confirm('确定要删除该角色？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        center: true,
        customClass: 'text-center'
      }).then(() => {
        this.$notify({
          title: '成功',
          message: '删除成功',
          type: 'success',
          duration: 2000
        })
        // this.$message({
        //   type: 'success',
        //   message: '删除成功!'
        // })
      }).catch(() => {

      })
      this.list.splice(index, 1)
    }
  }
}
</script>
<style scoped>
  [class^=el-icon-], [class*=" el-icon-"]{
    font-size: 16px;
  }
  .small-padding i{
    margin-right: 12px;
  }
  .small-padding i:last-child{
    margin-right: 0px;
  }
</style>
